﻿Ext.define('app.prm.detail.EditProgress',
{
    extend: 'Ext.window.Window',
    border: true,
    title: localize('EditStatus'),
    width: 550,
    modal: true,
    shadow: true,
    autoHeight: true,
    projectId: 0,
    initComponent: function () {
        var me = this;
        // this.addEvents("savesuccess");
        this.items = [this.getForm()];
        this.buttons = [
            {
                text: localize('Save'),
                handler: function () { this.submit(); }, scope: this
            },
            {
                text: localize('Cancel'),
                handler: function () { this.close(); }, scope: this
            }
        ];
        me.callParent();
    },
    listeners:
    {
        afterrender: function (me) {
            App.AjaxGet('/api/services/app/project/GetLastProgressOutPut?projectId=' + me.projectId, function (data) {
                me.form.getForm().setValues(data.result);
                me.getRadioGroup().setValue({ status: data.result.statusVal });
                me.getProgressGroup().setValue({ pg: data.result.progressVal });
                me.getWeekRadioGroup().setValue({ pg: data.result.reportWeek });
            });
        }
    },
    getForm: function () {
        var me = this;
        if (!this.form) {
            this.form = new Ext.form.FormPanel(
            {
                border: false,
                frame: false,
                bodyStyle: 'padding:5px',
                defaults: {
                    xtype: 'displayfield',
                    labelAlign: 'right',
                    width: '98%'
                },
                items: [
                    { xtype: 'hidden', name: 'projectId', value: me.projectId },
                    {
                        xtype: 'container',
                        anchor: '100%',
                        layout: 'hbox',
                        defaults: {
                            labelAlign: 'right'
                        },
                        items: [
                            this.getStartTime(),
                            this.getEndTime()
                        ]
                    },
                    this.getRadioGroup(),
                    this.getProgressGroup(),
                    this.getWeekRadioGroup(),
                    this.getTextArea()
                ]
            });
        }
        return this.form;
    },
    getStartTime: function () {
        var main = this;
        if (!this.startTime) {
            this.startTime = new Ext.form.DateField(
                {
                    flex: 1,
                    name: 'reportTime',
                    format: 'Y-m-d',
                    allowBlank: false,
                    editable: false,
                    value: new Date(),
                    fieldLabel: localize('ReportDate'),
                }
            );
        }
        return this.startTime;
    },
    getEndTime: function () {
        var main = this;
        console.log('**************');
        console.log(main.statusVal);
        console.log('**************');
        if (!this.endTime) {
            this.endTime = new Ext.form.DateField(
                {
                    flex: 1,
                    name: 'actualEndTime',
                    format: 'Y-m-d',
                    enableKeyEvents: true,
                    allowBlank: false,
                    disabled: true,
                    value: this.EndTime,
                    fieldLabel: localize('RequiredComplete')
                }
            );
        }
        return this.endTime;
    },
    getTextArea: function () {
        if (!this.textarea) {
            this.textarea = new Ext.form.TextArea({
                height: 130,
                name: 'note',
                value: this.processResult,
                fieldLabel: '描述'
            });
        }
        return this.textarea;
    },
    getStatus: function (_name) {
        if (!_name)
        { _name = "rado_status"; }
        var radios = this.find("name", _name);
        var status = "";
        for (var i = 0; i < radios.length; i++) {
            var rdo = radios[i];
            var checked = rdo.getValue();
            if (checked) {
                status = rdo.boxLabel;
            }
        }
        return status;
    },
    getWeekRadioGroup: function () {
        if (!this.weekradiogroup) {
            this.weekradiogroup = new Ext.form.RadioGroup(
            {
                fieldLabel: '客户周报',
                // columns: 5,
                vertical: true,
                items: [
                    { boxLabel: '单周报', name: 'ReportWeek', inputValue: '1', checked: true },
                    { boxLabel: '双周报', name: 'ReportWeek', inputValue: '2' }
                ]
            });
        }
        return this.weekradiogroup;
    },
    submit: function () {
        var me = this;
        var formValues = me.form.getForm().getValues();

        var status = me.getRadioGroup().getChecked()[0].inputValue;
        var progressStatus = me.getProgressGroup().getChecked()[0].inputValue;
        var reportWeek = me.getWeekRadioGroup().getChecked()[0].inputValue;
        formValues.status = status;
        formValues.progressStatus = progressStatus;
        formValues.reportWeek = reportWeek;
        formValues["actualEndTime"] = me.endTime.getValue();

        if (me.form.getForm().isValid()) {
            App.AjaxPost('/api/services/app/project/CreateProjectProgress', formValues, function (data) {
                me.fireEvent("savesuccess", formValues);
            });
        }



    },
    getRadioGroup: function () {
        if (!this.radiogroup) {
            this.radiogroup = new Ext.form.RadioGroup(
            {
                fieldLabel: localize('Status'),
                // columns: 5,
                vertical: true,
                items: [
                    { boxLabel: localize('Ready'), name: 'status', inputValue: 50, checked: true },
                    { boxLabel: localize('Processing'), name: 'status', inputValue: 10 },
                    { boxLabel: localize('Complete'), name: 'status', inputValue: 20 },
                    { boxLabel: localize('Pause'), name: 'status', inputValue: 30 },
                    { boxLabel: localize('End'), name: 'status', inputValue: 40 }
                ]
            });
        }
        return this.radiogroup;
    },
    getProgressGroup: function () {
        if (!this.progressgroup) {
            this.progressgroup = new Ext.form.RadioGroup(
            {
                fieldLabel: localize('ProgressStatus'),
                vertical: true,
                items: [
                    { boxLabel: localize('Normal'), name: 'pg', inputValue: 10, checked: true },
                    { boxLabel: localize('Delay'), name: 'pg', inputValue: 20 },
                    { boxLabel: localize('Advance'), name: 'pg', inputValue: 30 }
                ]
            });
        }
        return this.progressgroup;
    }
});